package com.phantasy.scene.sceneUnit.baseUnit.things.resource
{
	import com.phantasy.Style;
	import com.phantasy.globals.GameConfig;
	import com.phantasy.loaders.gameLoader.GameLoader;
	import com.phantasy.loaders.gameLoader.ResourceItem;
	import com.phantasy.scene.sceneUnit.baseUnit.things.common.BitmapClipData;
	
	import flash.display.BitmapData;
	import flash.display.GradientType;
	import flash.display.Loader;
	import flash.display.MovieClip;
	import flash.display.Shape;
	import flash.events.DataEvent;
	import flash.events.EventDispatcher;
	import flash.geom.Matrix;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.system.ApplicationDomain;
	import flash.utils.Dictionary;
	
	public class SourceManager extends EventDispatcher 
	{
		
		public static const AVATAR_CREATE_COMPLETE:String = "AvatarCreateComplete";
		public static const THING_CREATE_COMPLETE:String = "ThingCreateComplete";
		
		private static var _instance:SourceManager;
		
		private var cache_key:Vector.<String>;
		private var cache_value:Vector.<BitmapClipData>;
		private var _shadow:BitmapData;
		private var _mountShadow:BitmapData;
		private var _silhouette:BitmapData;
		private var loadingDic:Dictionary;
		
		
		public function SourceManager()
		{
			this.init();
		}
		
		public static function getInstance():SourceManager
		{
			if (_instance == null)
			{
				_instance = new (SourceManager)();
			}
			return (_instance);
		}
		
		private function init():void
		{
			this.cache_key = new Vector.<String>;
			this.cache_value = new Vector.<BitmapClipData>;
			this.loadingDic = new Dictionary();
			
		}
		
		public function getResource(_arg1:String):BitmapClipData
		{
			var _local2:int = this.cache_key.indexOf(_arg1);
			if (_local2 != -1)
			{
				return (this.cache_value[_local2]);
			}
			return null;
		}
		
		public function getShadow():BitmapData
		{
			var _local1:Matrix;
			var _local2:Shape;
			if (!this._shadow)
			{
				_local1 = new Matrix();
				_local1.createGradientBox(54, 28);
				_local2 = new Shape();
				_local2.graphics.beginGradientFill(GradientType.RADIAL, [0, 0], [0.4, 0], [150, 0xFF], _local1, "pad", "rgb", 0);
				_local2.graphics.drawEllipse(0, 0, 54, 28);
				_local2.graphics.endFill();
				this._shadow = new BitmapData(_local2.width, _local2.height, true, 0xFFFFFF);
				this._shadow.draw(_local2);
			}
			return (this._shadow);
		}
		
		public function getMountShadow():BitmapData
		{
			var _local1:Matrix;
			var _local2:Shape;
			if (!this._mountShadow)
			{
				_local1 = new Matrix();
				_local1.createGradientBox(108, 28);
				_local2 = new Shape();
				_local2.graphics.beginGradientFill(GradientType.RADIAL, [0, 0], [0.4, 0], [150, 0xFF], _local1, "pad", "rgb", 0);
				_local2.graphics.drawEllipse(0, 0, 108, 28);
				_local2.graphics.endFill();
				this._mountShadow = new BitmapData(_local2.width, _local2.height, true, 0xFFFFFF);
				this._mountShadow.draw(_local2);
			};
			return (this._mountShadow);
		}
		
		/**
		 *获取剪影 
		 * @return 
		 * 
		 */		
		public function getSilhouette():BitmapData
		{
			var silhounetteData:BitmapData;
			if (!this._silhouette)
			{
				silhounetteData = Style.getUIBitmapData(GameConfig.VIEWUI_URL, "Transparent");
				silhounetteData.copyPixels(silhounetteData, new Rectangle(0, 0, 41, 100), new Point(-20, -100));
			}
			return (silhounetteData);
		}

		
		public function deleteLoadingDic():void
		{
			this.loadingDic = new Dictionary();
		}
		
		public function load(_arg1:String, _arg2:String):void
		{
			if (this.loadingDic[_arg1] == null)
			{
				this.loadingDic[_arg1] = _arg2;
				GameLoader.getInstance().add(_arg1, ResourceItem.PRIORITY_1, false, _arg1, this.onLoaderComplete);
			}
		}
		
		public function createBitmapClip(_arg1:String, _arg2:ApplicationDomain, _arg3:MovieClip):void
		{
			var _local4:BitmapClipData = new BitmapClipData();
			_local4.url = _arg1;
			_local4.domain = _arg2;
			_local4.source = _arg3;
			this.cache_key.push(_arg1);
			this.cache_value.push(_local4);
		}
		
		public function onLoaderComplete(_arg1:Loader, _arg2:String):void
		{
			var _local3:ApplicationDomain = _arg1.contentLoaderInfo.applicationDomain;
			var _local4:MovieClip = (_arg1.contentLoaderInfo.content as MovieClip);
			this.createBitmapClip(_arg2, _local3, _local4);
			var _local5:String = this.loadingDic[_arg2];
			if (_local5 == "avatar")
			{
				dispatchEvent(new DataEvent(AVATAR_CREATE_COMPLETE, false, false, _arg2));
			}
			else
			{
				dispatchEvent(new DataEvent(THING_CREATE_COMPLETE, false, false, _arg2));
			}
			delete this.loadingDic[_arg2];
		}
		
	}
}
